// 导入组件
import {
    createRouter,
    createWebHistory
} from 'vue-router'

//创建并暴露一个路由器
const routes = [{ //重定向
        path: '',
        redirect: '/login',
    },
    { //登陆路由
        path: '/login',
        name: 'Login',
        component: () => import('../Views/login/login.vue')
    },
    { //教师端路由
        path: '/teacher',
        name: 'teaIndex',
        component: () => import('../views/teacher/index.vue'),
        meta: {
            beLogin: true,
            roles: ['2']
        },
        children: [{
                path: 'coursePublish',
                name: '课程设计任务发布',
                component: () => import('../Views/teacher/root/coursePublish.vue')
            },
            {
                path: 'try',
                name: 'try',
                component: () => import('../Views/teacher/root/try.vue')
            },
            {
                path: 'courseBase',
                name: '课程设计任务题库',
                component: () => import('../Views/teacher/root/courseBase.vue')
            },
            {
                path: 'courseSchema',
                name: '课程设计任务教学大纲',
                component: () => import('../Views/teacher/root/courseSchema.vue')
            },
            {
                path: 'teaTaskDetail',
                name: '教师端课程设计任务详情',
                component: () => import('../Views/teacher/root/courseTaskDetail.vue')
            },
            {
                path: 'courseProTask',
                name: '课程设计过程任务已发列表',
                component: () => import('../Views/teacher/root/courseProTask.vue')
            },
            {
                path: 'courseProPublish',
                name: '课程设计过程任务发布表',
                component: () => import('../Views/teacher/root/courseProPublish.vue')
            },
            {
                path: 'courseProDetail',
                name: '课程设计过程任务详情',
                component: () => import('../Views/teacher/root/courseProDetail.vue')
            },
            {
                path: 'innerCampus',
                name: '校内实践',
                component: () => import('../Views/teacher/root/innerCampus.vue')
            },
            {
                path: 'innerPublish',
                name: '校内实践发布',
                component: () => import('../Views/teacher/root/innerPublish.vue')
            },
            {
                path: 'innerSchema',
                name: '课内实践课题教学大纲',
                component: () => import('../Views/teacher/root/innerSchema.vue')
            },
            {
                path: 'innerDetail',
                name: '教师端校内任务详情',
                component: () => import('../Views/teacher/root/innerDetail.vue')
            },
            {
                path: 'innerGetStatus',
                name: '校内教师端查看学生接受任务情况',
                component: () => import('../Views/teacher/root/innerGetStatus.vue')
            },
            {
                path: 'innerProTask',
                name: '校内实践实践过程任务',
                component: () => import('../Views/teacher/root/innerProTask.vue')
            },
            {
                path: 'innerProPublish',
                name: '校内实践过程任务发布',
                component: () => import('../Views/teacher/root/innerProPublish.vue')
            },
            {
                path: 'innerProDetail',
                name: '校内过程任务详情',
                component: () => import('../Views/teacher/root/innerProDetail.vue')
            },
            {
                path: 'score',
                name: '学生成绩',
                component: () => import('../Views/teacher/root/stuScore.vue')
            },
            {
                path: 'personal',
                name: '教师个人信息',
                component: () => import('../Views/teacher/root/personal.vue')
            },
            {
                path: 'inDataAnalysis',
                name: '校内教师数据分析',
                component: () => import('../Views/teacher/root/inDataAnalysis.vue')
            },
            {
                path: 'teaScoring',
                name: '校内教师打分',
                component: () => import('../Views/teacher/root/teaScoring.vue')
            },
            {
                path: '',
                redirect: '/teacher/courseSchema'
            }
        ]
    },
    { //学生端路由
        path: '/student',
        name: 'stuIndex',
        component: () => import('../views/student/index.vue'),
        meta: {
            beLogin: true,
            roles: ['1']
        },
        children: [{
                path: 'home',
                name: '首页',
                component: () => import('../Views/student/root/home.vue')
            },
            {
                path: 'courseTask',
                name: 'courseTask',
                component: () => import('../Views/student/root/courseTask.vue')
            },
            {
                path: 'courseTaskDetail',
                name: '课程设计任务详情',
                component: () => import('../Views/student/root/courseTaskDetail.vue')
            },
            {
                path: 'innerCampus',
                name: '学生查看校内课题',
                component: () => import('../Views/student/root/innerCampus.vue')
            },
            {
                path: 'stuInnerDetail',
                name: '学生查看校内课题详情',
                component: () => import('../Views/student/root/innerDetail.vue')

            },
            {
                path: 'teamTaskDetail',
                name: '学生查看小队课题详情',
                component: () => import('../Views/student/root/teamTaskDetail.vue')

            },
            {
                path: 'offCampus',
                name: '学生查看校外课题',
                component: () => import('../Views/student/root/offCampus.vue')
            },
            {
                path: 'personal',
                name: '学生个人信息',
                component: () => import('../Views/student/root/personal.vue')
            },
            {
                path: 'team',
                name: '组队',
                component: () => import('../Views/student/root/team.vue')
            },
            {
                path: 'teamInfo',
                name: '组队详情',
                component: () => import('../Views/student/root/teamInfo.vue')
            },
            {
                path: 'teamApply',
                name: '邀请详情',
                component: () => import('../Views/student/root/teamApply.vue')
            },
            {
                path: 'processTask',
                name: '过程任务',
                component: () => import('../Views/student/root/processTask.vue')
            },
            {
                path: 'peerAssessment',
                name: '学生互评',
                component: () => import('../Views/student/root/peerAssessment.vue')
            },
            {
                path: 'companyEvaluate',
                name: '企业评价',
                component: () => import('../Views/student/root/companyEvaluate.vue')
            },
            {
                path: 'myEvaluation',
                name: '我的评价',
                component: () => import('../Views/student/root/myEvaluation.vue')
            },
            {
                path: 'stuOffDetail',
                name: '学生校外课题详情',
                component: () => import('../Views/student/root/offDetail.vue')
            },
            {
                path: '',
                redirect: '/student/home',
            },
        ],
    },
    { //校外教师端路由
        path: '/outTeacher',
        name: '校外教师界面',
        component: () => import('../Views/outTeacher/index.vue'),
        meta: {
            beLogin: true,
            roles: ['3']
        },
        children: [{
                path: 'offCampus',
                name: '校外课题实践',
                component: () => import('../Views/outTeacher/root/offCampus.vue')
            },
            {
                path: 'outPublish',
                name: '校外课题发布',
                component: () => import('../Views/outTeacher/root/outPublish.vue')
            },
            {
                path: 'outSchema',
                name: '校外课题教学大纲',
                component: () => import('../Views/outTeacher/root/outSchema.vue')
            },
            {
                path: 'outDetail',
                name: '校外老师实践任务详情',
                component: () => import('../Views/outTeacher/root/outDetail.vue')
            },
            {
                path: 'outGetStatus',
                name: '教师端查看学生接受任务情况',
                component: () => import('../Views/outTeacher/root/outGetStatus.vue')
            },
            {
                path: 'outProTask',
                name: '校外实践实践过程任务',
                component: () => import('../Views/outTeacher/root/outProTask.vue')
            },
            {
                path: 'outProPublish',
                name: '校外实践过程任务发布',
                component: () => import('../Views/outTeacher/root/outProPublish.vue')
            },
            {
                path: 'outProDetail',
                name: '校外过程任务详情',
                component: () => import('../Views/outTeacher/root/outProDetail.vue')
            },
            {
                path: 'teaScoring',
                name: '校外老师打分',
                component: () => import('../Views/outTeacher/root/teaScoring.vue')
            },
            {
                path: 'outTeacherCenter',
                name: '校外老师个人中心',
                component: () => import('../Views/outTeacher/root/outTeacherCenter.vue')
            },
            {
                path: 'outDataAnalysis',
                name: '校外老师数据分析',
                component: () => import('../Views/outTeacher/root/outDataAnalysis.vue')
            },
            {
                path: '',
                redirect: '/outTeacher/offCampus'
            }
        ]
    },
    { //管理员端路由
        path: '/admin',
        name: '管理员',
        component: () => import('../Views/admin/index.vue'),
        meta: {
            beLogin: true,
            roles: ['4']
        },
        children: [{
                path: 'try',
                name: '文件上传测试',
                component: () => import('../Views/admin/data/try.vue')
            },
            {
                path: 'adminhome',
                name: '主页',
                component: () => import('../Views/admin/home.vue')
            },
            {
                path: 'usercenter',
                name: '个人中心',
                component: () => import('../Views/admin/userCenter.vue')
            },
            {
                path: 'allCourseTask',
                name: '课程设计任务管理',
                component: () => import('../Views/admin/root/allCourseTask.vue')
            },
            {
                path: 'courseDetail',
                name: '管理员端课程设计任务详情',
                component: () => import('../Views/admin/root/adCourseDetail.vue')
            },
            {
                path: 'taskBase',
                name: '管理员端题库详情',
                component: () => import('../Views/admin/data/taskBase.vue')
            },
            {
                path: 'innerDetail',
                name: '管理员校内实践课题详情',
                component: () => import('../Views/admin/root/adInnerDetail.vue')
            },
            {
                path: 'allInnerCampus',
                name: '校内课题实践管理',
                component: () => import('../Views/admin/root/allInnerCampus.vue')
            },
            {
                path: 'allOffCampuss',
                name: '校外课题实践管理',
                component: () => import('../Views/admin/root/allOffCampus.vue')
            },
            {
                path: 'companytable',
                name: '企业审查',
                component: () => import('../Views/admin/companyProcess.vue')
            },
            {
                path: 'classdata',
                name: '班级信息',
                component: () => import('../Views/admin/data/classdata.vue')
            },
            {
                path: 'studata',
                name: '学生信息',
                component: () => import('../Views/admin/data/stuData.vue')
            },
            {
                path: 'teadata',
                name: '教师信息',
                component: () => import('../Views/admin/data/teaData.vue')
            },
            {
                path: 'practask',
                name: '实践课题任务',
                component: () => import('../Views/admin/data/practicalTask.vue')
            },
            {
                path: 'praccourse',
                name: '实践课程信息',
                component: () => import('../Views/admin/data/practiceCourse.vue')
            },
            {
                path: 'trainplan',
                name: '培养方案',
                component: () => import('../Views/admin/data/trainPlan.vue')
            },
            {
                path: 'contract',
                name: '合同信息',
                component: () => import('../Views/admin/contract.vue')
            },
            {
                path: 'dataAnalysis',
                name: '数据分析',
                component: () => import('../Views/admin/dataAnalysis.vue')
            },
            {
                path: 'try',
                name: '文件预览尝试',
                component: () => import('../Views/admin/data/try.vue')
            },
            {
                path: 'studentEvaluate',
                name: '管理员看学生评价企业',
                component: () => import('../Views/admin/studentEvaluate.vue')
            },
            {
                path: 'companyData',
                name: '管理员管理企业',
                component: () => import('../Views/admin/data/companyData.vue')
            },
            {
                path: '',
                redirect: '/admin/adminhome'
            }
        ]
    },
    { //企业端路由
        path: '/company',
        name: '企业界面',
        component: () => import('../Views/company/index.vue'),
        meta: {
            beLogin: true,
            roles: ['5']
        },
        children: [{
                path: 'companyhome',
                name: '企业主页',
                component: () => import('../Views/company/home.vue')
            },
            {
                path: 'companyteacher',
                name: '企业教师信息',
                component: () => import('../Views/company/outTeacherData.vue')
            },
            {
                path: 'studentEvaluate',
                name: '学生评价企业',
                component: () => import('../Views/company/studentEvaluate.vue')
            },
            {
                path: '',
                redirect: '/company/companyhome'
            }
        ]
    },
    // {//路由传参用
    //     path: '/detail/:id', // 定义参数占位符 ":id"
    //     component: Detail,
    // },
]

const router = createRouter({
    history: createWebHistory(),
    routes
});



// 全局前置守卫
router.beforeEach((to, from, next) => {
    let visitor = JSON.parse(localStorage.getItem("visitor"));
    let user = JSON.parse(localStorage.getItem("user"));
    const beLogin = to.meta.beLogin;
    const roles = to.meta.roles;
    let role = ''

    if(visitor != null && visitor!= undefined ){

        role = visitor.accountIdentity
    }
    

    if (beLogin == true && to.path != "/login") {
        if (user == null || user == undefined) {
            ElMessage({
                message: "请先登录",
                type: "warning",
            });
            next({
                path: "/login",
            });
            return;
        } else if (user != null ) {
            // console.log(new Date(user.loginKeepDate));
            // console.log(new Date())
            if (new Date(visitor.keepDate).getTime() < new Date().getTime()) {
                ElMessage({
                    message: "登录过期",
                    type: "warning",
                });
                next({
                    path: "/login",
                });
                return;
            }
        }
    }

    if (roles == null) {
        next();
    } else if (roles.includes(role)) {
        next();
    } else {
        ElMessage({
            message: "身份不正确",
            type: "warning",
        });
        next({
            path: "/login",
        });
    }
});


//对外暴露
export default router;